/*******************************************************************************
 * 对所有service执行统一管理接口 对所有service执行\初始化操作
 * 1、只对已挂载的服务文件进行管理;2、初始化服务文件;3、启动服务文件;4、获取服务文件；5、停止运行服务文件；
 ******************************************************************************/
Ext.define('WebOs.Kernel.ServiceMgr', {
	serviceCache : null,
	constructor : function(config) {
		var me = this;
		Ext.applyIf(me, config);
		me.serviceCache = new Ext.util.MixedCollection();
	},

	initServiceByServiceFile : function(serFile) {
		var me = this;
		var sname = serFile.get('name');
		var service = me.serviceCache.get(sname);
		if (!service) {
			var serviceCls = serFile.get('serviceCls');
			if (!serviceCls) {
				log('[WARN]', 'can not find serviceCls for ' + sname);
				return;
			} else {
				var fileConfig = serFile.get('params') || {};
				Ext.applyIf(fileConfig, {
							pname : serFile.get('pname'),
							name : serFile.get('name'),
							text : serFile.get('text'),
							kernel : me.kernel
						});
				service = Ext.create(serviceCls, fileConfig);
				me.serviceCache.add(sname, service);
				serFile.set('status', Constants.FILE_STATUS_INITED);
			}
		}
	},

	startServiceByServiceFile : function(serFile, config) {
		var me = this;
		if (!serFile || serFile.get('serviceType') == Constants.SFS_TYPE_TOC
				|| serFile.get('serviceType') == Constants.SFS_TYPE_ACT) {
			Ext
					.log('Can not run the file! It is not exist or not service file!');
			return false;
		}
		if (serFile.get('status') == Constants.FILE_STATUS_NINIT) {
			me.initServiceByServiceFile(serFile);
		}

		if (serFile.get('status') == Constants.FILE_STATUS_INITED) {
			var service = me.serviceCache.get(serFile.get('name'));
			if (service && service[Constants.START_INTERFACE_NAME]) {
				service[Constants.START_INTERFACE_NAME].call(service, config);
				if (service.single) {
					serFile.set('status', Constants.FILE_STATUS_RUNNED);
				}
				return service;
			}
		}
		
		if(serFile.get('status') == Constants.FILE_STATUS_RUNNED){
			core.fireEvent('repeatstartservice',serFile,config);
		}
	},

	getServiceInterface : function(ser) {

	},

	stopServiceByServiceFile : function(serFile) {
		var me = this;
		if (!serFile || serFile.get('serviceType') == Constants.SFS_TYPE_TOC
				|| serFile.get('serviceType') == Constants.SFS_TYPE_ACT) {
			Ext
					.log('Can not stop the file! It is not exist or not service file!');
			return false;
		}
		if (serFile.get('status') == Constants.FILE_STATUS_RUNNED) {
			var service = me.serviceCache.get(serFile.get('name'));
			if (service && service[Constants.STOP_INTERFACE_NAME]) {
				service[Constants.STOP_INTERFACE_NAME].call(service, arguments);
				serFile.set('status',Constants.FILE_STATUS_INITED);
				return true;
			}
		}
	},

	startServiceByKey : function(key) {
		var me = this;
		var serFile = me.kernel.sfs.getFileByKey(key);
		return me.startServiceByServiceFile(serFile);
	},

	stopServiceByKey : function(key) {
		var me = this;
		var serFile = me.kernel.sfs.getFileByKey(key);
		me.stopServiceByServiceFile(serFile);
	},

	startServiceByClsName : function(serClsName) {
		var me = this;
		var serFile = me.kernel.sfs.getOneFileByCondition(function(item, key) {
					if (item && item.get('serviceCls') == serClsName)
						return true;
					return false;
				});
		return me.startServiceByServiceFile(serFile);
	},

	stopServiceByClsName : function(serClsName) {
		var me = this;
		var serFile = me.kernel.sfs.getOneFileByCondition(function(item, key) {
					if (item && item.get('serviceCls') == serClsName)
						return true;
					return false;
				});
		me.stopServiceByServiceFile(serFile);
	}
});